package qc.module.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.common.core.exception.QCPromptException;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.platform.dto.phone.OfficialCategoryDto;
import qc.module.platform.entity.QcOfficialCategory;
import qc.module.platform.mapper.QcOfficialCategoryMapper;
import qc.module.platform.repository.QcOfficialCategoryRepository;

import java.util.List;

/**
 * @projectName: qcdp
 * @package: qc.module.platform.service
 * @className: OfficialCategoryService
 * @version: 1.0
 */
@Service
public class OfficialCategoryService {

    private QcOfficialCategoryRepository repository;


    @Autowired
    public void setQcOfficialCategoryRepository(QcOfficialCategoryRepository repository) {
        this.repository = repository;
    }

    private OfficialPhoneService officialPhoneService;

    @Autowired
    public void setOfficialPhoneService(OfficialPhoneService officialPhoneService) {
        this.officialPhoneService = officialPhoneService;
    }


    /**
     * 新增公务号码分类
     *
     * @param dto: 新增信息
     * @return String 新增成功返回null，失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/9/28
     */
    public String add(OfficialCategoryDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("新增信息不能为空");
        if (StringUtils.isBlank(dto.getName()))
            return QCUnifyReturnValue.Warn("分类名称不能为空");
        if (hasNameExist(dto.getName(), 0x0))
            return QCUnifyReturnValue.Warn("分类名称已存在!");
        QcOfficialCategory qcOfficialCategory = QcOfficialCategoryMapper.MAPPER.OfficialCategoryDtoToEntity(dto);
        //生成记录ID，获取数据库表中的最大记录ID+1
        qcOfficialCategory.setId(genereateId());
        if (repository.insert(qcOfficialCategory) < 0x1)
            return QCUnifyReturnValue.Warn("新增信息失败");
        return QCUnifyReturnValue.Success();
    }


    /**
     * 修改公务号码分类信息
     *
     * @param dto: 修改信息
     * @return String 修改信息成功返回null，失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/9/28
     */
    public String update(OfficialCategoryDto dto) {
        //判断修改条件
        if (dto == null)
            return QCUnifyReturnValue.Warn("更新号码分类信息不能为空");
        //判断修改的分类在数据库中是否存在
        if (!hasIdExist(dto.getId()))
            return QCUnifyReturnValue.Warn("更新对象不存在");
        //判断更新名称是否为空
        if (StringUtils.isBlank(dto.getName()))
            return QCUnifyReturnValue.Warn("分类名称不能为空");
        //判断修改的名称是否重复
        if (hasNameExist(dto.getName(), dto.getId()))
            return QCUnifyReturnValue.Warn("分类名称已存在,名称不能相同!");
        QcOfficialCategory qcOfficialCategory = QcOfficialCategoryMapper.MAPPER.OfficialCategoryDtoToEntity(dto);
        if (repository.updateById(qcOfficialCategory) < 0x1)
            return QCUnifyReturnValue.Warn("修改分类信息失败");
        return QCUnifyReturnValue.Success();
    }


    /**
     * 按id删除指定公务电话分类.
     *
     * @param id:            公务电话分类id
     * @param isLogicDelete: 是否为逻辑删除，true为逻辑删除
     * @author QcCheng Tech
     * @date 2023/9/28
     */
    public String delete(int id, boolean isLogicDelete) throws QCPromptException {
        //1.判断id是否为空
        if (id < 1)
            throw new QCPromptException("删除时分类id不能为空");
        //2.判断号码分类下是否存在号码，有则提醒不能删除
        if (officialPhoneService.isCategoryHasPhones(id))
            throw new QCPromptException("当前分类下存在手机号，不能删除");
        //3.判断是否为逻辑删除
        if (isLogicDelete) {
            LambdaUpdateWrapper<QcOfficialCategory> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(QcOfficialCategory::getId, id);
            wrapper.set(QcOfficialCategory::getFlag, ResourceStatusFlagEnum.DELETED);
            if (repository.update(null, wrapper) < 0x1)
                return QCUnifyReturnValue.Warn("删除失败");
        } else {
            if (repository.deleteById(id) < 0x1)
                return QCUnifyReturnValue.Warn("删除失败");
        }
        return QCUnifyReturnValue.Success();
    }


    /**
     * 根据id查询公务电话分类信息
     *
     * @param id: 公务号码分类id
     * @return OfficialCategoryDto 公务号码分类信息
     * @author QcCheng Tech
     * @date 2023/9/28
     */
    public OfficialCategoryDto get(int id) throws QCPromptException {
        //1,判空
        if (id < 1)
            throw new QCPromptException("号码分类id不能为空");
        //2.查询
        QcOfficialCategory qcOfficialCategory = repository.selectById(id);
        //3.判断查询结果，有结果返回转换内容
        if (qcOfficialCategory != null) {
            return QcOfficialCategoryMapper.MAPPER.toDto(qcOfficialCategory);
        }
        return null;
    }

    /**
     * 获取全部分类
     *
     * @return List<OfficialCategoryDto> 分类结果
     * @author QcCheng Tech
     * @date 2023/10/7
     */
    public List<OfficialCategoryDto> getAll() {
        LambdaQueryWrapper<QcOfficialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcOfficialCategory::getId, QcOfficialCategory::getOdr, QcOfficialCategory::getFlag, QcOfficialCategory::getName);
        //排序先按排序号升序，再按名称升序
        wrapper.orderByAsc(QcOfficialCategory::getOdr, QcOfficialCategory::getName);
        List<QcOfficialCategory> qcOfficialCategories = repository.selectList(wrapper);
        //判断查询结果，有则转换后返回查询结果，无结果返回null
        if (!qcOfficialCategories.isEmpty()) {
            return QcOfficialCategoryMapper.MAPPER.toDtoList(qcOfficialCategories);
        }
        return null;
    }


    /**
     * 生成记录ID，获取数据库表中的最大记录ID+1
     *
     * @return 生成记录ID
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    Integer genereateId() {
        Integer maxRecordId = getMaxId();
        return maxRecordId + 1;
    }

    /**
     * 获取数据库表中的最大ID值，没有记录时返回0
     *
     * @return 返回数据库表中的最大ID值
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    Integer getMaxId() {
        LambdaQueryWrapper<QcOfficialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcOfficialCategory::getId);
        wrapper.orderByDesc(QcOfficialCategory::getId);
        QcOfficialCategory en = repository.selectOne(wrapper);
        if (en != null)
            return en.getId();
        return 0x0;
    }

    /**
     * 判断分类名称是否存在，存在返回true
     *
     * @param name: 判断分类名称
     * @return Boolean 判断分类名称存在返回true
     * @author QcCheng Tech
     * @date 2023/9/28
     */
    boolean hasNameExist(String name, int id) {
        LambdaQueryWrapper<QcOfficialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcOfficialCategory::getId);
        wrapper.eq(QcOfficialCategory::getName, name);
        wrapper.ne(QcOfficialCategory::getId, id);

        QcOfficialCategory qcOfficialCategory = repository.selectOne(wrapper);
        if (qcOfficialCategory != null)
            return true;
        return false;
    }

    /**
     * 判断数据库中指定分类是否存在
     *
     * @param id: 分类ID
     * @return boolean 存在返回true,不存在返回false
     * @author QcCheng Tech
     * @date 2023/10/11
     */
    boolean hasIdExist(int id) {
        LambdaQueryWrapper<QcOfficialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcOfficialCategory::getId);
        wrapper.eq(QcOfficialCategory::getId, id);

        QcOfficialCategory qcOfficialCategory = repository.selectOne(wrapper);
        if (qcOfficialCategory != null)
            return true;
        return false;

    }


}
